package org.apache.dolphinscheduler.api.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.ExampleDataService;
import org.apache.dolphinscheduler.api.service.OutputInfoService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static org.apache.dolphinscheduler.api.enums.Status.QUERY_COLUMN_INFO_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_OUTPUT_INFO_ERROR;
import static org.apache.dolphinscheduler.api.service.impl.OutputInfoServiceImpl.objectToArray;

@RestController
@RequestMapping("/output")
public class OutputInfoController extends BaseController {

    @Autowired
    OutputInfoService outputInfoService;
    @Autowired
    ExampleDataService exampleDataService;

    @GetMapping(value = "getColumnInfo")
    @ApiException(QUERY_COLUMN_INFO_ERROR)
    public Result<Object> getColumnInfo(int nodeId){
        Map<String, Object> map= outputInfoService.getColumnInfo(nodeId,1);
        return  returnDataList(map);
    }

    @GetMapping(value = "getInfo")
    @ApiException(QUERY_OUTPUT_INFO_ERROR)
    public Result<Object> getInfo(int nodeId,int point){
        Map<String, Object> map= outputInfoService.getList(nodeId,point);
        return  returnDataList(map);
    }

    @GetMapping(value = "/getColumnsById")
    @ResponseStatus(HttpStatus.OK)
    public Object getColumns(
            @RequestParam(value = "dataSourceType") int dataSourceType,
            @RequestParam(value = "dataSourceId") int dataSourceId
    ) throws Exception {
        Map<String, Object> result =exampleDataService.getColumnsById(dataSourceType,dataSourceId);
        return returnDataList(result);
    }

    @GetMapping(value = "getXyInfo")
    @ApiException(QUERY_OUTPUT_INFO_ERROR)
    public Result<Object> getXYInfo(int nodeId, int point, @RequestParam Object X, @RequestParam Object Y){

        String[] Xstr=objectToArray(X);
        String[] Ystr=objectToArray(Y);
        Map<String, Object> map= outputInfoService.getXYList(nodeId,point,Xstr,Ystr);
        return  returnDataList(map);
    }
}
